/**
 * Copyright 2011 Joao Miguel Pereira
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.medibooking.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.medibooking.gwt.client.calendar.views.CalendarView;
import com.medibooking.gwt.client.menu.CalendarOptionsWidget;
import com.medibooking.gwt.client.resources.CalendarResources;

/**
 * @author joaomiguel.pereira@gmail.com
 */
@Singleton
public class CalendarApplication {
	// ~~~~~~Instance vars ~~~~~

	private boolean isInitialized = false;
	private CalendarView calendarView;
	private CalendarOptionsWidget calendarOptions;

	/**
	 * Create new instances of CalendarApplication with required dependencies
	 * 
	 * @param calendarManager
	 * @param eventBus
	 * @param calendarView
	 * @param calendarOptions
	 */
	@Inject
	public CalendarApplication(CalendarView calendarView,
			CalendarOptionsWidget calendarOptions) {

		this.calendarView = calendarView;
		this.calendarOptions = calendarOptions;

	}

	/**
	 * Initialize application, can only be called once
	 */
	public void run() {
		if (this.isInitialized) {
			throw new IllegalStateException(
					"Can only call init one time in it's lifecycle");
		}
		init();
	}

	/**
	 * Initializae application
	 */
	private void init() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(calendarOptions);
		panel.add(calendarView);
		RootPanel.get("container").add(panel);

		GWT.<CalendarResources> create(CalendarResources.class).css()
				.ensureInjected();

	}

}
